package com.bjgov.scoresys.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.ObjectNotFoundException;

import com.bjgov.scoresys.dao.UserDAO;
import com.bjgov.scoresys.entity.User;
import com.bjgov.scoresys.resource.UserKey;
import com.bjgov.scoresys.util.Page;
import com.bjgov.scoresys.util.PageUtil;
import com.bjgov.scoresys.util.Result;

public class UserHandler {
	// private static final Log log = LogFactory.getLog(UserHandler.class);
	private static UserDAO dao = new UserDAO();

	public static User getUser(String loginName) {
		return dao.getUserByName(loginName);
	}

	public static User getUser(int id) {
		return dao.findById(id);
	}

	public static void saveUser(User user) {
		dao.save(user);
	}

	public static List<User> getScoreUsers() {
		return dao.findByAuthority(UserKey.Authority_User);
	}

	public static List<User> getUsersByIds(String ids) {
		if (StringUtils.isEmpty(ids))
			return new ArrayList<User>();
		return dao.findByIds(ids);
	}

	public static Result list(Page page) throws HibernateException,
			ObjectNotFoundException {
		int totalRecords = dao.count();
		page = PageUtil.createPage(page, totalRecords);
		List list = dao.getByPage(page);
		return new Result(list, page);
	}
}
